<template>
  <transition :name="dir === 'prev' ? 'indexL' : 'indexR'">
    <div
      :style="ItemSize ? CustomItemSize : BigItemSize"
      class="car-item c-index"
      v-if="selfIndex === currentIndex"
    >
      <slot></slot>
    </div>
  </transition>

  <transition :name="dir === 'prev' ? 'prev' : ''">
    <div
      :style="ItemSize ? CustomItemSize : BigItemSize"
      class="car-item c-left"
      v-if="
        (selfIndex - 1 <= -1 ? itemLen - 1 : selfIndex - 1) === currentIndex
      "
    >
      <slot></slot>
    </div>
  </transition>
  <transition :name="dir === 'next' ? 'next' : ''">
    <div
      :style="ItemSize ? CustomItemSize : BigItemSize"
      class="car-item c-right"
      v-if="(selfIndex + 1 >= itemLen ? 0 : selfIndex + 1) === currentIndex"
    >
      <slot></slot>
    </div>
  </transition>
</template>

<script>
import { getCurrentInstance, reactive, watch, toRefs, onMounted } from "vue";
export default {
  name: "CarItem",
  props: {
    ItemSize: {
      type: Object,
      default: null,
    },
  },
  setup(props) {
    const instance = getCurrentInstance();
    const state = reactive({
      selfIndex: instance.vnode.key, //获取自身index
      itemLen: instance.parent.setupState.getItemLen(),
      dir: "",
      currentIndex: instance.parent.ctx.currentIndex,
      CustomItemSize: {},
      BigItemSize: {
        "--Itemwidth": "100%",
        "--Itemheight": "100%",
        "--cItemWidth": "50%",
        "--cItemHeight": "80%",
        "--cItemLeft": "-5%",
        "--cItemRight": "55%",
      },
    });

    onMounted(() => {
      state.CustomItemSize = props.ItemSize;
    });

    watch(
      () => {
        // 返回监听对象
        return [instance.parent.ctx.currentIndex, instance.parent.ctx.dir];
      },
      (value) => {
        state.currentIndex = value[0];
        state.dir = value[1];
      }
    );
    return {
      ...toRefs(state),
    };
  },
};
</script>

<style lang="scss" scoped>
.car-item {
  cursor: pointer;
  position: absolute;
  top: 0%;
  left: 0%;
  width: var(--Itemwidth);
  height: var(--Itemheight);
}
.c-index {
  z-index: 1;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.c-left,
.c-right {
  width: var(--cItemWidth);
  height: var(--cItemHeight);
  top: 10%;
}
.c-left {
  left: var(--cItemLeft);
}
.c-right {
  left: var(--cItemRight);
}

.indexL-enter-active {
  animation: bounce-left 0.3s;
}
.indexL-leave-active {
  animation: bounce-prev-leave 0.3s;
}

.indexR-enter-active {
  animation: bounce-right 0.3s;
}
.indexR-leave-active {
  animation: bounce-next-leave 0.3s;
}

.prev-enter-active {
  animation: prev-leave 0.3s;
}
.next-enter-active {
  animation: next-leave 0.3s;
}

@keyframes next-leave {
  0% {
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    top: 10%;
    left: var(--cItemRight);
    z-index: 1;
  }
  100% {
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    top: 10%;
    left: var(--cItemLeft);
    z-index: -1;
  }
}
@keyframes prev-leave {
  0% {
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    top: 10%;
    left: var(--cItemLeft);
    z-index: 1;
  }
  100% {
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    top: 10%;
    left: var(--cItemRight);
    z-index: -1;
  }
}

@keyframes bounce-right {
  0% {
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    top: 10%;
    left: var(--cItemLeft);
    transform: translate(0%, 0%);
    z-index: 2;
  }
  100% {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: var(--Itemwidth);
    height: var(--Itemheight);
    z-index: 2;
  }
}
@keyframes bounce-next-leave {
  0% {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: var(--Itemwidth);
    height: var(--Itemheight);
  }
  100% {
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    top: 10%;
    left: var(--cItemRight);
    transform: translate(0%, 0%);
  }
}

@keyframes bounce-left {
  0% {
    top: 10%;
    left: var(--cItemRight);
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    transform: translate(0%, 0%);
    z-index: 2;
  }
  100% {
    top: 50%;
    left: 50%;
    width: var(--Itemwidth);
    height: var(--Itemheight);
    transform: translate(-50%, -50%);
    z-index: 2;
  }
}
@keyframes bounce-prev-leave {
  0% {
    top: 50%;
    left: 50%;
    width: var(--Itemwidth);
    height: var(--Itemheight);
    transform: translate(-50%, -50%);
  }
  100% {
    top: 10%;
    left: var(--cItemLeft);
    width: var(--cItemWidth);
    height: var(--cItemHeight);
    transform: translate(0%, 0%);
  }
}
</style>